// search.js
var Trim = require('../../utils/util.js');
var app = getApp();
var url = app.globalData.url;
var headers;
var keyword, next_page_token;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lineList: [],//线路列表
    showLoading: true,//控制数据加载提示开关
    init:false,
    city:"深圳",
    empty:false,
    url:"https://app1.sztravel.com.cn/file/weixinapp/img"
  },
  inject:function(e){
      keyword=e.detail.value;
      console.log(keyword)
  },
  navto: function (e) {
    wx.navigateTo({
      url: '../line_detail/line_detail?lineId=' + e.currentTarget.dataset.id + '',
    });
  },
  request:function(flag){
    console.log('request')
    var that=this;
    var req = '{"city":"","dest_id":0,"dest_level":0,"time_start":"","time_end":"","days":0,"sort_type":0,"main_type":0,"next_page_token":"' + next_page_token+'"}'
    wx.request({
      url: url +'api/search_line_by_key',
      method:"POST",
      header:headers,
      data:{
        "req":encodeURIComponent(req),
        "next_page_token":""+next_page_token+"",
        "keyword":""+keyword+""
      },
      success:function(data){
       if(!flag){
         var lineList = that.data.lineList;
       }else{
         var lineList=[]
         if (!data.data.data.line_list){
           var empty=true
         }else{
           var empty = false
         }
       }
        if(data.data.result===1){
          next_page_token = data.data.data.next_page_token;
          if (next_page_token){
            var showLoading=true
          }else{
            var showLoading = false
          }
          lineList.push.apply(lineList, data.data.data.line_list);
          console.log(lineList)
          that.setData({
            lineList: lineList,
            showLoading: showLoading,
            init:true,
            empty: empty
          })
        }
      }
    });
  },
  
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    headers=app.globalData.headers;
    next_page_token=""
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {//到底部加载  
  
    var that = this
    if (next_page_token) {//存在下一页
      that.request(false);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})